/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.science_item.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.science_item.entity.TbScienceItem;
import com.thinkgem.jeesite.modules.science_item.service.TbScienceItemService;

/**
 * 项目Controller
 * @author hang
 * @version 2017-01-14
 */
@Controller
@RequestMapping(value = "${adminPath}/science_item/tbScienceItem")
public class TbScienceItemController extends BaseController {

	@Autowired
	private TbScienceItemService tbScienceItemService;
	
	@ModelAttribute
	public TbScienceItem get(@RequestParam(required=false) String id) {
		TbScienceItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbScienceItemService.get(id);
		}
		if (entity == null){
			entity = new TbScienceItem();
		}
		return entity;
	}
	
	@RequiresPermissions("science_item:tbScienceItem:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbScienceItem tbScienceItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbScienceItem> page = tbScienceItemService.findPage(new Page<TbScienceItem>(request, response), tbScienceItem); 
		model.addAttribute("page", page);
		return "modules/science_item/tbScienceItemList";
	}

	@RequiresPermissions("science_item:tbScienceItem:view")
	@RequestMapping(value = "form")
	public String form(TbScienceItem tbScienceItem, Model model) {
		model.addAttribute("tbScienceItem", tbScienceItem);
		return "modules/science_item/tbScienceItemForm";
	}

	@RequiresPermissions("science_item:tbScienceItem:edit")
	@RequestMapping(value = "save")
	public String save(TbScienceItem tbScienceItem, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbScienceItem)){
			return form(tbScienceItem, model);
		}
		tbScienceItemService.save(tbScienceItem);
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+Global.getAdminPath()+"/science_item/tbScienceItem/?repage";
	}
	
	@RequiresPermissions("science_item:tbScienceItem:edit")
	@RequestMapping(value = "delete")
	public String delete(TbScienceItem tbScienceItem, RedirectAttributes redirectAttributes) {
		tbScienceItemService.delete(tbScienceItem);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/science_item/tbScienceItem/?repage";
	}

}